package main

import (
	"log"
	"time"
	"yundian/internel/app/helper"
	"yundian/internel/app/models/prices"
	"yundian/internel/pkg/dbs"
	"yundian/internel/pkg/dingo"
	"yundian/internel/pkg/dingo/global"
	"yundian/internel/pkg/utils"
)

type PlaceIdNameList struct {
	Id          int    `gorm:"column:id" json:"id"`
	PlaceName   string `gorm:"column:place_name" json:"placeName"`
}

type PlaceIdList struct {
	PlaceId     int    `gorm:"column:place_id" json:"placeId"`
}

// 获取所有门店 feeMode 1 计时  2套餐
func GetPlaceListByFeeMode(feeMode int) []PlaceIdList {
	var lists []PlaceIdList
	sql := `select place_id from devices where place_id > 0 and fee_mode = ? group by place_id`
	global.Orm.Raw(sql, feeMode).Find(&lists)
	return lists
}

// 获取价格 - 计时
func GetPriceInfoByPlaceId(placeId int) *prices.PriceModel {
	priceInfo := &prices.PriceModel{}
	sql := `select * from prices where place_id = ? and device_type_id = 10 and create_at > 0 and place_id > 0 order by create_at desc limit 1`
	global.Orm.Raw(sql, placeId).Find(priceInfo)
	return priceInfo
}

func InsertIntoPlacePrice(m *prices.PriceModel) bool {
	db := global.Orm.Debug().Table("prices").Create(m)
	if db.Error != nil {
		return false
	}
	return true
}

func main() {
	dingo.InitViper("./configs/adm.yaml")
	dbs.NewMysqlComponent().Step()
	lists := GetPlaceListByFeeMode(1)
	//lists2 := GetPlaceListByFeeMode(2)
	var data,data8,data10,data12 []int
	for _,lst := range lists {
		priceInfo := GetPriceInfoByPlaceId(lst.PlaceId)
		if priceInfo != nil {
			if priceInfo.Deposit < 1000 { // 小于10押金
				if priceInfo.Deposit == 800 {
					priceInfo.Id = 0
					priceInfo.Deposit = 1500
					priceInfo.MaxPayPrice = 1500
					priceInfo.CreateAt = dingo.Time(time.Now().Unix())
					priceInfo.Name = "2022年春节"
					priceInfo.ProductId = utils.Str2Md5(helper.GenerateUUID())
					if b := InsertIntoPlacePrice(priceInfo); b {
						data8 = append(data8, priceInfo.PlaceId)
					}
				} else {
					data = append(data, priceInfo.PlaceId)
				}
			} else {
				if priceInfo.Deposit == 1000 {
					priceInfo.Id = 0
					priceInfo.Deposit = 1500
					priceInfo.MaxPayPrice = 1500
					priceInfo.CreateAt = dingo.Time(time.Now().Unix())
					priceInfo.Name = "2022年春节"
					priceInfo.ProductId = utils.Str2Md5(helper.GenerateUUID())
					if b := InsertIntoPlacePrice(priceInfo); b {
						data10 = append(data10, priceInfo.PlaceId)
					}
				}
				if priceInfo.Deposit == 1200 {
					priceInfo.Id = 0
					priceInfo.Deposit = 1500
					priceInfo.MaxPayPrice = 1500
					priceInfo.CreateAt = dingo.Time(time.Now().Unix())
					priceInfo.Name = "2022年春节"
					priceInfo.ProductId = utils.Str2Md5(helper.GenerateUUID())
					if b := InsertIntoPlacePrice(priceInfo); b {
						data12 = append(data12, priceInfo.PlaceId)
					}
				}
			}
		}
	}
	log.Println("data", data)
	log.Println("data8-押金为8元的门店", data8)
	log.Println("data10-押金为10元的门店", data10)
	log.Println("data12-押金为12元的门店", data12)
}